﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.OleDb;

public partial class admin_menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void SysMenu() {
        StringBuilder html = new StringBuilder();
        html.Append("<table width=\"180\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
        if (Session["admin"] != null)
        {
            OleDbConnection conn = DB.conn();
            OleDbConnection conn2 = DB.conn();
            if (conn != null && conn2!=null)
            {
                conn.Open();
                conn2.Open();
                OleDbCommand comm = new OleDbCommand();
                comm.Connection = conn;
                OleDbCommand comm2 = new OleDbCommand();
                comm2.Connection = conn2;
                //qiang 为最高管理员，显示所有
                if ("qiang".Equals(Session["admin"].ToString()))
                {
                    //提取一级菜单
                    comm.CommandText = "select * from sys_menu where par_id is null or par_id=0 order by menuOrder";
                    OleDbDataAdapter adapter = new OleDbDataAdapter();
                    adapter.SelectCommand = comm;
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    for (int i = 0; i < dt.Rows.Count;i++ )
                    {
                        html.Append("<tr>\n");
                        html.Append("<td height=\"25\" align=\"center\" valign=\"middle\" background=\"images/left_xbj2.gif\">\n");
                        html.Append("<table width=\"170\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
                        html.Append("<tr>\n");
                        html.Append("<td width=\"20\" height=\"25\" align=\"center\" valign=\"middle\">\n");
                        html.Append("<img src=\"images/left_dot3.gif\" width=\"3\" height=\"6\">\n");
                        html.Append("</td>\n");
                        html.Append("<td height=\"25\" align=\"left\" valign=\"middle\" class=\"hy_dhwz\" onClick=\"showMenu(" + i + ")\">\n");
                        html.Append("<a href=\"#\">" + dt.Rows[i]["menuName"].ToString() + "</a>\n");
                        html.Append("</td>\n");
                        html.Append("</tr>\n");
                        html.Append("<tr>\n");
                        html.Append("<td align=\"left\" id=\"submenu" + i + "\"  style=\"display:none\" colspan=\"2\">\n");
                        html.Append("<div id=\"mode0\">\n");
                        html.Append(" <table width=\"180\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");

                        //二级菜单
                        comm2.CommandText = "select menuUrl,menuName from sys_menu where par_id=" + dt.Rows[i]["id"].ToString() + " order by menuOrder";
                        OleDbDataAdapter adapter2 = new OleDbDataAdapter();
                        adapter2.SelectCommand = comm2;
                        DataTable dt2 = new DataTable();
                        adapter2.Fill(dt2);
                        for (int j = 0; j < dt2.Rows.Count; j++)
                        {
                            html.Append("<tr>\n");
                            html.Append("<td width=\"30\" height=\"25\" align=\"center\" valign=\"middle\" background=\"images/left_xbj1.gif\"><img src=\"images/left_dot1.gif\" width=\"12\" height=\"12\">\n");
                            html.Append("</td>\n");
                            html.Append("<td height=\"25\" align=\"left\" valign=\"middle\" background=\"images/left_xbj1.gif\" class=\"hy_dq\">\n");
                            html.Append("<a href=\"" + dt2.Rows[j]["menuUrl"].ToString() + "\" target=\"mainFrame\">" + dt2.Rows[j]["menuName"].ToString() + "</a>\n");
                            html.Append("</td>\n");
                            html.Append("</tr>\n");
                        }

                        html.Append("</table>\n");
                        html.Append("</div>\n");
                        html.Append("</td>\n");
                        html.Append("</tr>\n");
                        html.Append("</table>\n");
                        html.Append("</td>\n");
                        html.Append("</tr>\n");
                    }
                }
                else
                {
                    //提取一级菜单
                    comm.CommandText = "select * from sys_menu where (par_id is null or par_id=0) and id in (select menuId from role_menu where roleId="+ Session["roleId"].ToString()+ ") and menuShow=1 order by menuOrder";
                    OleDbDataAdapter adapter = new OleDbDataAdapter();
                    adapter.SelectCommand = comm;
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        html.Append("<tr>\n");
                        html.Append("<td height=\"25\" align=\"center\" valign=\"middle\" background=\"images/left_xbj2.gif\">\n");
                        html.Append("<table width=\"170\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
                        html.Append("<tr>\n");
                        html.Append("<td width=\"20\" height=\"25\" align=\"center\" valign=\"middle\">\n");
                        html.Append("<img src=\"images/left_dot3.gif\" width=\"3\" height=\"6\">\n");
                        html.Append("</td>\n");
                        html.Append("<td height=\"25\" align=\"left\" valign=\"middle\" class=\"hy_dhwz\" onClick=\"showMenu(" + i + ")\">\n");
                        html.Append("<a href=\"#\">" + dt.Rows[i]["menuName"].ToString() + "</a>\n");
                        html.Append("</td>\n");
                        html.Append("</tr>\n");
                        html.Append("<tr>\n");
                        html.Append("<td align=\"left\" id=\"submenu" + i + "\"  style=\"display:none\" colspan=\"2\">\n");
                        html.Append("<div id=\"mode0\">\n");
                        html.Append(" <table width=\"180\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");

                        //二级菜单
                        comm2.CommandText = "select * from sys_menu where par_id=" + dt.Rows[i]["id"].ToString() + "  and id in (select menuId from role_menu where roleId=" + Session["roleId"].ToString() + ") and menuShow=1 order by menuOrder";
                        OleDbDataAdapter adapter2 = new OleDbDataAdapter();
                        adapter2.SelectCommand = comm2;
                        DataTable dt2 = new DataTable();
                        adapter2.Fill(dt2);
                        for (int j = 0; j < dt2.Rows.Count; j++)
                        {
                            html.Append("<tr>\n");
                            html.Append("<td width=\"30\" height=\"25\" align=\"center\" valign=\"middle\" background=\"images/left_xbj1.gif\"><img src=\"images/left_dot1.gif\" width=\"12\" height=\"12\">\n");
                            html.Append("</td>\n");
                            html.Append("<td height=\"25\" align=\"left\" valign=\"middle\" background=\"images/left_xbj1.gif\" class=\"hy_dq\">\n");
                            html.Append("<a href=\"" + dt2.Rows[j]["menuUrl"].ToString() + "\" target=\"mainFrame\">" + dt2.Rows[j]["menuName"].ToString() + "</a>\n");
                            html.Append("</td>\n");
                            html.Append("</tr>\n");
                        }

                        html.Append("</table>\n");
                        html.Append("</div>\n");
                        html.Append("</td>\n");
                        html.Append("</tr>\n");
                        html.Append("</table>\n");
                        html.Append("</td>\n");
                        html.Append("</tr>\n");
                    }
                }
                conn.Close();
                conn2.Close();
            }
        }
        html.Append(" </table>\n");
        Response.Write(html.ToString());
    }
}
